home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / searchPathArray.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  4.8 KB  |  179 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //
  22. //<doc>
  23. //<name searchPathArray>
  24. //<owner "Alias|Wavefront Unsupported">
  25. //
  26. //<synopsis>
  27. //        searchPathArray( string $name, string $pathArray[] )
  28. //
  29. //<returns>
  30. //        string Full path to file.
  31. //
  32. //<description>
  33. //        Looks for a readable file in the directories listed in pathArray.
  34. //<P>
  35. //    Notes:<br>
  36. //        Intial white space in $name is ignored (chopped)<br>
  37. //        Name beginning with "/" are returned as-is<br>
  38. //        Empty string entries in pathArray are skipped, use . for cwd.<br>
  39. //        Path elements beginning in "$" are treated as environment variables.
  40. //
  41. //<flags>
  42. //        string $name Name of the file being searched for
  43. //        string $pathArray[] Array of directory paths.
  44. //
  45. //<examples>
  46. //    string $path[] = { ".", "$HOME/maya/projects/default" };
  47. //    searchPathArray( "workspace.mel", $path );
  48. //
  49. //</doc>
  50.  
  51. proc int existsReadable(string $filename)
  52. {
  53.     int $debug = 0;
  54.     string $func  = "existsReadable";
  55.  
  56.     if ( $debug ) trace("// " + $func + "( " + $filename + " )\n" );
  57.  
  58. //    int $id = fopen($filename,"r");
  59. //    if ( $id != 0 ) {
  60. //        fclose($id);
  61. //    }
  62.     int $id = filetest("-r",$filename);
  63.  
  64.     if ( $debug ) trace("// " + $func + " return " + ( $id != 0 ) + " \n" );
  65.  
  66.     return ( $id != 0 );
  67. }
  68.  
  69. proc string chopGetenv(string $envNameWithDollar)
  70. {
  71. //    int $debug = 1;
  72. //    string $func  = "chopGetenv";
  73.  
  74.     string $chopped = substitute("^\\$", $envNameWithDollar, "" );
  75. //    if ( $debug ) trace("// " + $func + ": chopped:" + $chopped + "\n");
  76.     return getenv( $chopped );
  77. }
  78.     
  79.  
  80. proc string expandEnv(string $path)
  81. //    Description:
  82. //        Expand file name paths containing environment variables
  83. //
  84. //    Notes:
  85. //        We assume that any whole component of the path can be
  86. //         an environment variable.  This means we can parse
  87. //        /usr/people/$USER/foo  but not
  88. //        /usr/people/$USER.foo/bar
  89. {
  90.     int $debug = 0;
  91.     string $func  = "expandEnv";
  92.  
  93.     if ( $debug ) trace("// " + $func + "( " + $path + " )\n" );
  94.  
  95.     // If no $ sign, no expansion        
  96.     if ( "" == match("\\\$",$path ) ) {
  97.         if ( $debug ) trace(" Tried to match \\\$\n" );
  98.         if ( $debug ) trace("// " + $func + ": not env.\n");
  99.         return $path;
  100.     }
  101.     
  102.     // If no /, simple variable name only
  103.     if ( "" == match("/",$path ) ) {
  104.         if ( $debug ) trace("// " + $func + ": simple expansion\n");
  105.         
  106.         return chopGetenv($path);
  107.     }
  108.  
  109.     // Aha! the path is comprised of literal and env components...
  110.     if ( $debug ) trace("// " + $func + ": multi part\n");
  111.  
  112.     string $result = "";
  113.  
  114.     if ( match("^/",$path ) != "" )
  115.         $result = "/";
  116.         
  117.     string $parts[];
  118.     tokenize( $path, "/", $parts );
  119.  
  120.     // Reconstruct the path, expanding the env components
  121.     string $dir;
  122.     for ($dir in $parts) {
  123.         if ( "" != match("^\\\$",$dir ) ) {
  124.             $dir =  chopGetenv($dir);
  125.             // fail on any unset envs
  126.             if ( $dir == "" ) {
  127.                 if ( $debug ) trace("// " + $func + ": env part not set\n");
  128.                 return "";
  129.             }
  130.         }
  131.  
  132.         $result = $result + "/" + $dir;
  133.     }
  134.         
  135.     return $result;
  136.         
  137. }
  138.  
  139. global proc string searchPathArray( string $name, string $pathArray[] )
  140. {
  141.     int $debug = 0;
  142.     string $func  = "searchPathArray";
  143.  
  144.     // strip off leading white space
  145.     $name = substitute("^[     ]*", $name, "" );
  146.  
  147.     // return empty names directly
  148.     if ($name == "" )
  149.         return $name;
  150.  
  151.     // return absolute paths directly
  152.     if ( match("^/",$name ) != "" ) {
  153.         if ($debug) trace("// " + $func + ": absolute path\n");
  154.         return $name;
  155.     }
  156.     // Search for $name in the path list
  157.     string $fullPath;
  158.     for ( $dir in $pathArray ) {
  159.         // Expand environment variables
  160.         if ( $debug ) trace("// " + $dir + ": before expansion\n");
  161.         $dir = expandEnv($dir);
  162.         if ( $debug ) trace("// " + $dir + ": after expansion\n");
  163.  
  164.         // Skip empty path entries
  165.         if ( "" == $dir )
  166.             continue;
  167.  
  168.         // strip off trailing white space
  169.         $dir = substitute("[     ]*$", $dir, "" );
  170.  
  171.         // Test this path and return if successful
  172.         $fullPath = $dir + "/" + $name;
  173.         if ( existsReadable($fullPath) )
  174.             return ($fullPath);    
  175.     }
  176.  
  177.     return "";
  178. }
  179.